package net.tsingyun.samples.web;

import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import net.tsingyun.commons.core.page.PageResponse;
import net.tsingyun.commons.core.page.Paging;
import net.tsingyun.commons.core.page.Response;
import net.tsingyun.commons.libs.Queryable;
import net.tsingyun.samples.domain.Sample;
import net.tsingyun.samples.dto.SampleDTO;
import net.tsingyun.samples.service.SampleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 简单示例|简单示例相关接口
 *
 * @author : liuyk@tsingyun.net
 */
@RestController
public class SampleController {
    @Resource
    private SampleService sampleService;

    /**
     * 新增简单示例|简单示例
     * 
     * @param dto	 dto
     * @return Response<Long>
     **/
    @PostMapping("/add/samples")
    public Response<Long> create(@RequestBody @Validated SampleDTO dto) {
        Sample e = this.sampleService.create(dto);
        return Response.ok(e.getId());
    }

    /**
     * 批量新增简单示例|简单示例
     * 
     * @param dtoList	 dtoList
     * @return Response<Boolean>
     **/
    @PostMapping("/add/batch/samples")
    public Response<Boolean> create(@RequestBody @Validated List<SampleDTO> dtoList) {
        this.sampleService.create(dtoList);
        return Response.ok();
    }

    /**
     * 更新简单示例|简单示例
     * 
     * @param id	 id
     * @param dto	 dto
     * @return Response<Long>
     **/
    @PostMapping("/update/samples/{id}")
    public Response<Long> update(@PathVariable Long id, @RequestBody @Validated SampleDTO dto) {
        Sample e = this.sampleService.update(id, dto);
        return Response.ok(e.getId());
    }

    /**
     * 删除简单示例|简单示例
     * 
     * @param id	 id
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/samples/{id}")
    public Response<Boolean> delete(@PathVariable Long id) {
        this.sampleService.delete(id);
        return Response.ok();
    }

    /**
     * 删除简单示例|简单示例
     * 
     * @param ids	 ids
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/batch/samples")
    public Response<Boolean> delete(@RequestBody List<Long> ids) {
        this.sampleService.delete(ids);
        return Response.ok();
    }

    /**
     * 查询简单示例|简单示例（通过ID）
     * 
     * @param id	 id
     * @return Response<Sample>
     **/
    @GetMapping("/info/samples/{id}")
    public Response<Sample> byId(@PathVariable Long id) {
        Optional<Sample> optional = this.sampleService.getById(id);
        return Response.ok(optional.orElse(null));
    }

    /**
     * 查询简单示例|简单示例（列表）
     * 
     * @param query	 query
     * @return Response<List<Sample>>
     **/
    @GetMapping("/list/samples")
    public Response<List<Sample>> list(Queryable query) {
        List<Sample> entities = this.sampleService.listByWhere(query);
        return Response.ok(entities);
    }

    /**
     * 查询简单示例|简单示例（分页）
     * 
     * @param query	 query
     * @param paging	 paging
     * @return Response<PageResponse<Sample>>
     **/
    @GetMapping("/page/samples")
    public Response<PageResponse<Sample>> page(Queryable query, Paging paging) {
        PageResponse<Sample> response = this.sampleService.listByWhere(query, paging);
        return Response.ok(response);
    }
}